package com.lx.servlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.lx.dao.ResumeDao;
import com.lx.entity.Resume;

@WebServlet("/uploadpic.do")
@MultipartConfig(
		location="/", fileSizeThreshold=1024*1024, 
		maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)
public class ResumeUpdatePicServlet extends HttpServlet {
	 private static final long serialVersionUID = 1L;

	    /**
	     * @see HttpServlet#HttpServlet()
	     */
	    public ResumeUpdatePicServlet() {
	        super();
	        // TODO Auto-generated constructor stub
	    }
	    /**
	     * .
	     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	     *      response)
	     */
	    protected void doPost(HttpServletRequest request, HttpServletResponse response)
	            throws ServletException, IOException {
			Part part = request.getPart("headShot");
/*	    	String header = part.getHeader("content-disposition");
	    	String fileName = header.substring(header.indexOf("filename=\"")+10,header.lastIndexOf("\""));
	    	String newFileName = System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));//创建一个新的文件名
	    	String filepath = getServletContext().getRealPath("/applicant/images");//设置路径
*/	    	
	    	
	    	
	        // 获取request的所有的请求参数(将请求参数转换为Part)
	        Collection<Part> list = request.getParts();
	        String fileName = null;
//	        for(Part p: list) {
	            // 获取上传的文件名称
	            String filename = part.getSubmittedFileName();
	            // 创建要保存的文件对象
	            fileName = createName(filename);
	            File file = new File(createDir(getServletContext()), fileName);
	            // 保存文件
	            part.write(file.getAbsolutePath());
//	        }
	        
	        ResumeDao redao =  new ResumeDao();
	        HttpSession session = request.getSession();
	        int basicinfoid = (int) session.getAttribute("basecinfoid");
	        redao.updateHeadShot(basicinfoid, fileName);
	        Resume resumes = new Resume();
			resumes = redao.queryBasec(basicinfoid);	
			request.setAttribute("resumes",resumes);
			request.getRequestDispatcher("applicant/resume.jsp").forward(request, response);
	    }

	    // 创建目录---以日期，一天一个文件夹
	    private File createDir(ServletContext context) {
	        String filepath = context.getRealPath("/file/images");
	        //File file = new File(filepath, new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()));
	        File file = new File(filepath);
	        if (!file.exists()) {
	            file.mkdirs();
	        }
	        return file;
	    }

	    // 创建文件名--区分同名文件,在文件名前加上当前的时间
	    private String createName(String name) {
	        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime()) + "_" + name;
	    }
}
